From: Martin Brown
To: <john.becker@wimborne.co.uk>
Subject: PIC and mix
Date: 28 April 2004 08:07

Dear John,

This is a quick note covering a selection of PIC related topics from
recent (and not so recent) EPE Readout. But mainly to congratulate EPE
on adding a new PIC n' Mix column in May for microcontroller development
ideas.

I was a bit surprised that you prefer crash and burn "real-time" testing
of PIC code running directly on a PIC. I find the power of MPSIM (even
the old DOS version) invaluable for quick development of new complex
code that can be tested in isolation and with easy access to all
variables. I might be interested in writing an article on using MPSIM if
you think it would be of general interest.

I built the delightful Icebreaker project a couple of years ago and that
is another good stepping stone for prototyping. I would like to gain
access to the C-code running at the PC end of the link to make a few
small improvements to the user interface and add an extra P command to
step over a function call. I would be happy to sign an NDA and return
the modified code. It is for personal hobby use.

I have recently been playing with direct drive LCD for a very low
current continuous display. I wanted to make a sidereal rate clock for
astronomy (which runs about 0.3% faster than mean solar time). This low
power design uses a 32kHZ crystal, 16F877 driving an RS 0.6" 4 digit LCD
display driven directly - drawing only 22uA total current on 3v. I doubt
there is much call for sidereal clocks, but the LCD display design might
still be of interest.  It will run for a couple of years on two spent AA
cells. It also has a novel digital fine adjustment by slightly altering
the minute loop timing in 1ppm steps.

Here is the computed GOTO that provides a mechanism for a handy variable
cycle delay loop. Starting with the very simplest one - which is very
space consuming for longer delays:

; Simple variable cycle delay loop for  8+W cycles where 0 <  W < 8 in
this example.

DLY8_W  ANDLW 7         ; mask W to match length of table
           XORLW  7             ; complement so that
                                ; W=7 means 7 cycle extra delay
           ADDWF  PCL,F ; computed goto
           NOP  ; 7
           NOP  ; 6
           NOP  ; 5
           NOP  ; 4
           NOP  ; 3
           NOP  ; 2
           NOP  ; 1
           RETURN


; Variable cycle delay loop for 17+W cycles by PCL arithmetic ; The
computed jump must be located in the same page ; Tramples W, COUNT
register

DLY17_W MOVWF  COUNT    ; execute delay loop for 17+N cycles
          RRF      COUNT,F
          RRF    COUNT,F        ; compute W/4
          BCF    COUNT,7
              BCF    COUNT,6    ; mask out top bits to zero 0<COUNT<64
          INCF   COUNT,F        ; add 1 (so that delay=0 is shortest)
          BCF    STATUS,C ; no carry
          ANDLW  3              ; W contains the remainder after /4
          XORLW  3              ; complement to get offset for PCL jump
          ADDWF  PCL,F  ; variable delay by 1-4 cycles
          NOP
          NOP
DLY_4W NOP
          DECFSZ COUNT,F  ; variable delay by 4n cycles
          GOTO   DLY_4N   ; above is a delay loop for 17-272 cycles
          RETURN

The design could be adapted to do almost anything that requires a
continuous LCD display and as the code is largely table driven it can
fairly easily handle either 4 digit or 31/2 digit displays. An LCR meter
would be a candidate if there hadn't been one published fairly recently.

By way of a simple illustration here is the code for computing  2^W  by
table lookup.

SAFE_2W ANDLW 7         ; mask W to be 0<W<8
UNSAFE_2W       ADDLW PCL,F             ; faster but unsafe entry point
                                        ; caller must ensure 0<W<8
                RETLW 1         ; 2^0
                RETLW 2         ; 2^1 etc.
                RETLW 4
                RETLW 8
                RETLW 16
                RETLW 32
                RETLW 64
                RETLW 128

Similar table based code is used to compute the 7 segment masks and to
set and unset display bits. Obviously such tables must never span a 256
byte code boundary. Here is a routine to set or clear the bit in PORTA
specified by the lowest 3 bits of W

; Carry = 1 means set,  Carry = 0 means clear
; Valid bit numbers mean 0 < W < 8 on entry

BITFSR  MOVWF TEMP  ; again 0 < W < 8
                                ;  we mask later to make certain
        RLF       TEMP,F        ; 2W+C
        RLF        TEMP,W       ; 4W+2C
        ANDLW  01EH             ; 0 < = 4W+2C <= 30
        ADDWF  PCL,F            ; computed goto into the table to do the
work
        BCF        PORTA,0      ; W=0, C=0 - clear bit 0
        RETURN
        BSF       PORTA,0       ; W=0, C=1 - set bit 0
        RETURN
        BCF       PORTA,1       ; W=1, C=0 - clear bit 1
        RETURN
         etc
        BSF       PORTA,7       ; W=7, C=1 - set bit 7
        RETURN

This routine uses up a fair amount of space but including the call
overhead takes only 11 cycles independent of which bit is being changed.

The recent article on HIGH and PCLATH will perhaps encourage other
people to make greater use of table driven code in higher pages. There
is usually plenty of spare code space available in the newer chips so
that by using  PCLATH and arithmetic on the program counter faster but
more bulky code can be used. And sometimes the replacement code of this
type may be both shorter and faster.

I hope these code snippets are of some interest. If the sidereal clock
project or some derivative (it also can keep mean solar time) is of any
interest for publication in EPE then please let me know and I will tidy
it up into presentable form. My prototype is built on veroboard.

Although I am a professional software developer, I do not think you
should drift towards tutorials on various programming languages like VB,
VC++ etc. There are more than enough computer magazines already and only
a handful of electronics ones remaining. Your magazine space is too
precious to waste on material that has been covered extensively
elsewhere. I would be a bit more sympathetic towards cross compiling
high level languages onto embedded micros and simple PC interfacing.

Best regards,
Martin Brown

PS I thought the seismometer project was excellent too. Keep up the good
work!
-- 
Martin Brown

Email has been scanned by www.emf-systems.com for viruses and SPAM 